use rand::Rng;
use std::{cmp::Ordering, io};

fn main() {
    println!("- guess the number -");
    let secret_number = rand::rng().random_range(1..=100);

    loop {
        println!("Please input your guess.(1~100)");

        let mut guess = String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        let guess = match guess.trim().parse::<u8>() {
            Ok(n @ 1..=100) => n,
            _ => continue,
        };

        println!("You guessed: {guess}");

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}
